﻿namespace PillBox.Client.ViewModels
{
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Linq;
    using PillBox.Client.Framework;
    using PillBox.Client.Model;

    [Export]
    public class MedicationViewModel : ViewModelBase
    {
        public MedicationViewModel()
        {
        }

        public IEnumerable<MedicationChoiceViewModel> Medications
        {
            get;
            set;
        }

        public override void Activate()
        {
            ResultEnumerator.Begin(this.LoadPrescriptions());
        }

        private IEnumerable<IResult> LoadPrescriptions()
        {
            var query = new GetMedicationQuery().AsResult();

            yield return query;

            this.Medications = query.Response.AsMedicationChoice().OrderBy(x => x.Medication.Name);

            RaisePropertyChanged(() => Medications);
        }
    }
}